﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace EntityframeworkDemo
{
    public static class IQueryableExtensions
    {
        public static IQueryable<TSource> TimeRangeWhere<TSource>(this IQueryable<TSource> source, TimeRange range, Expression<Func<TSource, DateTime?>> selector)
        {
            if (range != null)
            {
                var time = selector.Compile();
                if (range.StartTime != null)
                {
                    source = source.Where(c => range.StartTime <= time(c));
                }
                if (range.EndTime != null)
                {
                    source = source.Where(c => time(c) <= range.EndTime);
                }
            }
            return source;
        }
    }
}
